package cn.com.jslh.nfcpro.base;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.json.JSONArray;

/**
 * 作者：周洲 on 2017/3/8 09:38
 * <p>
 * 邮箱：double0zhou@126.com
 */

public abstract class BaseJSonArrayRecycleViewAdapter extends RecyclerView.Adapter<SuperViewHolder> {
    private OnItemClickListener onItemClickListener;
    private OnItemClickListener onItemLongClickListener;
    private JSONArray datalist =  new JSONArray();
    @Override
    public SuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(),parent,false);
        return new SuperViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SuperViewHolder holder, final int position) {
        onBindItemHolder(holder,position);
        if(onItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v,position);
                }
            });
        }
        if (onItemLongClickListener!=null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemLongClickListener.onItemClick(v,position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datalist.length();
    }

    /**
     * 添加
     * @param datalist
     */
    public void setDataList(JSONArray datalist) {
        this.datalist = new JSONArray();
        this.datalist = datalist;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemClickListener onItemLongClickListener){
        this.onItemLongClickListener  = onItemLongClickListener;
    }

    public JSONArray getDataList(){
        return this.datalist;
    }

    public abstract int getLayoutId();

    public abstract void onBindItemHolder(SuperViewHolder holder, int position);


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
